//
//  DetailOverlayLayer.m
//  Asteroids
//
// This class implements a CCLayer that is to be overlayed onto the AsteroidsLayer.
// This layer is used to show certain information like the userName and currentScore.
//
//  Created by charles@kollar.com on 11/10/2010.
//
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#import "DetailOverlayLayer.h"
#import "ApplicationData.h"

#define LABEL_FONT_SIZE					14
// Best to choose a fixed width font
#define LABEL_FONT						@"Courier"


@implementation DetailOverlayLayer

@synthesize userNameLabel;
@synthesize currentScoreLabel;
@synthesize livesImageView;


- (id)init
{ 
	if ((self=[super init])) {
		CGSize screenSize = [[CCDirector sharedDirector] displaySize];
		// Need to swap these because the device is used in landscape orientation...
		int sWidth = screenSize.height;
		int sHeight = screenSize.width;
		
		// Here we are using CCLable rather than a 'UITextField' on an 'openGLView'.
		// The reason is that the CCLabel does what we need and is simpler to work with...
		// IMPORTANT NOTE: the method that we are calling to get a CCLable does an autorelease
		// so we need to retain it, or risk having it removed from under our noses.
		userNameLabel = [[CCLabel labelWithString:@"Name" fontName:LABEL_FONT fontSize:LABEL_FONT_SIZE] retain];
		userNameLabel.position =  ccp(20, sHeight-LABEL_FONT_SIZE); // Top left hand corner (device used in landscape)
		// The userName is set in the 'SaveScoreLayer' when the user selects a row in the table and
		// before the user asks the CCDirector to transisition to a scene containing this CCLayer,
		// so this string should have been defined at this point.
		[userNameLabel setString:[[ApplicationData shared] userName]];
		[self addChild: userNameLabel];
		
		currentScoreLabel = [[CCLabel labelWithString:@"Score" fontName:LABEL_FONT fontSize:LABEL_FONT_SIZE] retain];
		currentScoreLabel.position =  ccp(sWidth-14*5, sHeight-LABEL_FONT_SIZE);
		[self addChild: currentScoreLabel];
		
		// Max umber of lives that the player can get...
        // create them all here, but only make them visible as needed...
        livesImageView = [[NSMutableArray alloc] initWithCapacity: MAX_LIVES];
		for (int i=1; i<=MAX_LIVES; i++) {
            CCSprite *sprite = [CCSprite spriteWithFile:@"life.png"];
            [self addChild:sprite];
            CGSize size = sprite.contentSize;
            [sprite setPosition:ccp(100+size.width*i, sHeight-size.height/2)];
            [livesImageView addObject: sprite];
            [sprite setVisible: NO];
		}
    }
    return self;
}

// The 'draw' method of a CCLayer is called when the layer is drawn.
// We will specialize this method to change the labels on this layer to the values
// set by current game play (these are externs set elsewhere in the game).
// Here we will change the string associated with the labels on this layer to the current values
- (void)draw
{	
	// The currentScore is set in the 'AsteroidsLayer' when the user shoots and hits an asteroid.
	[currentScoreLabel setString:[NSString stringWithFormat:@"%d(%d)",
								  [[ApplicationData shared] currentScore],
								  [[ApplicationData shared] getHighScore]]];
	
	// Show as many lives are there are left...
	NSEnumerator *e = [livesImageView objectEnumerator];
	for (int i=1; i<=MAX_LIVES; i++) {
		CCSprite *sprite = [e nextObject];
		[sprite setVisible: (i <= [[ApplicationData shared] lives]) ? YES : NO];
	}
	
	[super draw];
}

- (void)dealloc
{
	NSLog(@"DetailOverlayLayer dealloc\n");
    [userNameLabel release];
	[currentScoreLabel release];
	[livesImageView release];
    
	[super dealloc];
}

@end
